/*
 * display.c
 *
 *  Created on: Mar 30, 2012
 *      Author: Tri_D
 */

#include <mc9s08ll64.h>
#include "display.h"
#include "font.h"
#include "map.h"
#include "main header.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"


void DisplayAllOn(void)
{	
	uint8_t * pR = & LCDWF3;
	uint8_t c;
	for(c = 3; c <= 34; c++)
	{
		*(pR) = 0x07;
		pR++;
	}
}


void DisplayAllOff(void)
{
	uint8_t * pR = &LCDWF3;
	uint8_t c;
	for(c = 3; c <= 34; c++)
	{
		*pR = 0;
		pR++;
	}
}


// Display decimal point
void Display_Dcp(uint8_t pos)
{
	*(Dcp_Reg[pos]) |= 0x04; 
}



// Display msg on the 6 seven segment digits
void Display_String(uint8_t * pd, uint16_t * pannuc)
{
	uint8_t c, c1, c2;
	uint8_t ch = 0;
	uint8_t dcp_ct = 0;
	
	c2 = 0;										// this index keeps track of what character to process
	// Clear left and right colon
	*(pannuc) &= ~LEFT_COLON;
	*(pannuc) &= ~RIGHT_COLON;
	for(c = 0; c < MAX_DIGIT; c++)
	{
		// Convert the character to bit mask
		ch = *(pd + c2);
		if (ch == '.' || ch == ',')				// Check for decimal point
		{
			dcp_ct++;
			Display_Dcp(c2-dcp_ct);
			c--;								// Don't count this character in digit display
		}
		else if (ch == ':')						// Check for colon
		{
			if (c2 == 2)
			{
				*(pannuc) |= LEFT_COLON;
			}
			else if (c2 == 5)
			{
				*(pannuc) |= RIGHT_COLON;
			}
			c--;								// Don't count this character in digit display
		}
		else
		{
			ch = FONT_S7[ch - ' '];
			// Map the seven segment character
			for(c1 = 0; c1 < 7; c1++)
			{			
				if(ch & (1<<c1))				// Check if that bit set
				{
					// Mapping array start from least significant bit as segment G
					*(D_Reg[6-c1+7*c]) |= (1<<Digit_S[6-c1]);
				}			 
			}
		}
		c2++;
	}	
}



// Display unit 
void Display_Unit(uint8_t * pd)
{
	uint8_t c, c1;
	uint8_t ch = 0;
	uint8_t l_count_ = 0;
	
	for(c = 0; c < MAX_UNIT_CHAR; c++)
	{
		ch = 0;
		if (*(pd+c) == ' ')											
			{ch = FONT_UNIT[0];}				// S_U
		if ((*(pd+c) == 'l' || *(pd+c) == 'L') && !l_count_)		
			{ch = FONT_UNIT[1]; l_count_++;}	// L1_U			// L2_U
		if (*(pd+c) == 'k' || *(pd+c) == 'K')						
			{ch = FONT_UNIT[2];}				// K_U			
		if (*(pd+c) == '<')											
			{ch = FONT_UNIT[7];}				// LEFTARROW_UNIT_U
		if (*(pd+c) == 'o' || *(pd+c) == 'O')						
			{ch = FONT_UNIT[4];}				// O_U
		if (*(pd+c) == 'b' || *(pd+c) == 'B')						
			{ch = FONT_UNIT[3];}				// B_U
		if (*(pd+c) == 'g' || *(pd+c) == 'G')						
			{ch = FONT_UNIT[5];}				// G_U
		if (*(pd+c) == 'Z' || *(pd+c) == 'z')						
			{ch = FONT_UNIT[6];}				// Z_U
		// Convert the character to bit mask
		if (ch)
		{
			// Map the unit character 
			for(c1 = 0; c1 < MAX_UNIT_BIT; c1++)
			{
				if (ch & (1<<c1))						// Check if that bit set
				{
					*(U_Reg[MAX_UNIT_BIT-1-c1]) |= (1<<U_S[MAX_UNIT_BIT-1-c1]);		// Turn that bit on
				}
			}
		}
	}
}
                                


// Display Annunciation
void Display_Annuc(uint16_t Annuc)
{
	uint8_t c;
	
	for(c = 0; c < MAX_ANNUC_BIT; c++)
	{
		if (Annuc & (1<<c))							// Check if that bit set
		{
			*(AN_Reg[MAX_ANNUC_BIT-1-c]) |= (1<<AN_S[MAX_ANNUC_BIT-1-c]);			// turn that bit on
		}
	}
}



// Display mode
void Display_Mode(uint16_t Mode)
{
	uint8_t c;
	uint16_t ch = 0;
	
	for(c = 0; c < MAX_MODE_BIT; c++)		
	{		
		ch = FONT_S14[Mode - ' '];
		if(ch & (1<<c))				// Check if that bit set
		{
			*(M_Reg[MAX_MODE_BIT-1-c]) |= (1<<M_S[MAX_MODE_BIT-1-c]);	// Turn that bit on
		}
	}
}



// Display battery. Accept level = 0-5
void Display_Bat(uint8_t level)
{
	
	if (level >= 1)						// No bar			
	{
		*(B_Reg[0]) |= 0x01;				
	}
	if (level >= 2)						// one bar
	{
		*(B_Reg[4]) |= 0x01;
	}
	if (level >= 3)						// two bar
	{
		*(B_Reg[3]) |= 0x01;
	}
	if (level >= 4)						// three bar 
	{
		*(B_Reg[2]) |= 0x01;
	}	
	if (level >= 5)						// four bar
	{
		*(B_Reg[1]) |= 0x01;
	}
}
